
from clickhouse_driver import Client
import pandas as pd


class ClickhoustClient:
    def __init__(self, host='localhost', port=9000, user='default',
                 password='Keepgo123@cys', database='db_cfx'):
        self.client = Client(
            host=host,
            port=port,
            user=user,
            password=password,
            database=database,
            settings={'use_numpy': True}  # 可选，提高性能
        )
        self.database = database

    def query(self, query: str, params: dict = None) -> pd.DataFrame:

        try:
            result = self.client.query_dataframe(query, params)
            return result
        except Exception as e:
            return pd.DataFrame()

# 使用示例
if __name__ == "__main__":
    

    # 方法1: 常规使用
    client = ClickhoustClient()
    data = client.query("select * from  db_cfx.t_monitor_log_collect_compare where date=today() limit 10 ;")

    print("数据:",data)
    for row in data:
        print('列名：',row)
        print('数据',data[row])



